package com.example.justbuy.action.admin;

import com.example.justbuy.service.*;
import com.example.justbuy.pojo.*;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/")
public class CheckAction {

    @Autowired
    WorkflowService workflowService;

    @Autowired
    StoreDaoService storeDaoService;

    @Autowired
    ImageMapperService imageMapperService;
    /**
     * 进入商家审核页面
     * @param model 模型
     * @return
     */
    @RequestMapping("shoppingcheck.htm")
    public String shoppingcheck(Model model){
        List<Task> tasks=workflowService.getTasks("商家审核");
        List<HashMap> listh=new ArrayList<HashMap>();
        HashMap map=new HashMap();
        if (tasks!=null&&tasks.size()>0){
            for (Task task : tasks) {
                map.put("taskid",task.getId());
                Map map1=workflowService.getVariablesByTask(task.getId());
                map.put("realname",map1.get("realname"));
                map.put("taskname",task.getName());
                map.put("addtime",task.getCreateTime());
                listh.add(map);
            }
        }
        model.addAttribute("listh",listh);
        return "admin/shoppingcheck.html";
    }

    /**
     * 查看具体的商家信息
     * @param model
     * @param taskid
     * @return
     */
    @RequestMapping("showshopping.htm")
    public String showshopping(Model model,String taskid){
        Map map=workflowService.getVariablesByTask(taskid);
        map.put("taskid",taskid);
        String path="../"+(String) map.get("path");
        map.put("path",path);
        model.addAttribute("map",map);
        return "admin/showshopping.html";
    }

    /**
     * 进行审核，通过改变数据库的参数
     * 不通过则存入不通过的原因
     * 只要审核完毕就跳入商家审核页面
     * @param model
     * @param why
     * @return
     */
    @RequestMapping("showshopping1.htm")
    public String showshopping1(Model model,String why,String taskid){
        Map<String,Object> map=workflowService.getVariablesByTask(taskid);
        String did=workflowService.getProcessInstanceid(taskid);
        workflowService.completeTask(taskid,"",map);
        if(why!=null&&!why.equals("")){
            map.put("why",why);
            map.put("storeState","未通过");
        }else{
            map.put("storeState","已通过");
        }
        storeDaoService.updateStore(map);
        taskid=workflowService.getTaskid(did,"发送请求结果");       //storeDaoService;
        workflowService.completeTask(taskid,"",map);
        List<Task> tasks=workflowService.getTasks("商家审核");
        model.addAttribute("tasks",tasks);
        return "admin/shoppingcheck.html";
    }
    /**
     * 进入商品审核页面
     * @param model 模型
     * @return
     */
    @RequestMapping("goodscheck.htm")
    public String goodscheck(Model model){
        List<Task> tasks=workflowService.getTasks("商品审核");
        List<HashMap> listt=new ArrayList<HashMap>();
        HashMap map=new HashMap();
        if (tasks!=null&&tasks.size()>0){
            for (Task task : tasks) {
                map.put("taskid",task.getId());
                Map map1=workflowService.getVariablesByTask(task.getId());
                map.put("companyName",map1.get("companyName"));
                map.put("taskname",task.getName());
                map.put("addtime",task.getCreateTime());
                listt.add(map);
            }
        }
        System.out.println(listt.size());
        model.addAttribute("listc",listt);
        return "admin/goodscheck.html";
    }

    /**
     * 查看具体的商品信息
     * @param model
     * @param taskid
     * @return
     */
    @RequestMapping("showgoods.htm")
    public String showgoods(Model model,String taskid){
        Map map=workflowService.getVariablesByTask(taskid);
        map.put("taskid",taskid);
        String path="../"+(String) map.get("path");
        map.put("path",path);
        model.addAttribute("map",map);
        return "admin/showgoods.html";
    }

    /**
     * 进行审核，通过改变数据库的参数
     * 不通过则存入不通过的原因
     * 只要审核完毕就跳入商家审核页面
     * @param model
     * @param why
     * @return
     */
    @RequestMapping("showgoods1.htm")
    public String showgoods1(Model model,String why,String taskid){
        Map<String,Object> map=workflowService.getVariablesByTask(taskid);
        String did=workflowService.getProcessInstanceid(taskid);
        workflowService.completeTask(taskid,"",map);
        if(why!=null&&!why.equals("")){
            map.put("why",why);
            map.put("goodsState","未通过");
        }else{
            map.put("goodsState","出售中");
        }
        taskid=workflowService.getTaskid(did,"发送请求结果");       //storeDaoService;
        workflowService.completeTask(taskid,"",map);
        imageMapperService.updateGoods(map);
        List<Task> tasks=workflowService.getTasks("商品审核");
        model.addAttribute("tasks",tasks);
        return "admin/goodscheck.html";
    }
}
